package com.tools.hry.xls.create.rule;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

/**
 * Created by 峥桂 on 2017/9/6.
 */
public class ExcelStyle {

    private Workbook workbook;
    private InputStream tplInStream ;
    private static final String templateName="template.xls";
    private static final String templateName2007="template.xlsx";



    private CellStyle titleStyle;
    private CellStyle titleStyle_;
    private CellStyle titleStyle_1;
    private CellStyle titleStyle_2;
    private CellStyle headerStyle;
    private CellStyle contentStyle;

    public ExcelStyle(String selfTemplatePath,boolean isXSSFWorkbook) {
        System.out.println(System.getProperty("user.dir"));
        try {
            _setTemplateInputStream(selfTemplatePath,isXSSFWorkbook);
            if(tplInStream!=null){
//                POIFSFileSystem fs = new POIFSFileSystem(tplInStream);
                if (isXSSFWorkbook) {
                    workbook = new XSSFWorkbook(tplInStream);
                }else{
                    workbook = new HSSFWorkbook(tplInStream);
                }

//                if (POIFSFileSystem.hasPOIFSHeader((InputStream)inp)) {
//                    return new HSSFWorkbook((InputStream)inp);
//                } else if (POIXMLDocument.hasOOXMLHeader((InputStream)inp)) {
//                    return new XSSFWorkbook(OPCPackage.open((InputStream)inp));
//                } else {
//                    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
//                }
//
                if(workbook==null){
                    workbook=WorkbookFactory.create(tplInStream);
                }
                tplInStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  void initStyle(Workbook currWorkbook){

        if(workbook==null){
            return;
        }
        Sheet sheet=workbook.getSheetAt(0);
        int lastRowNum= sheet.getLastRowNum();
        Row row;String key;CellStyle value;
        for(int i=1;i<lastRowNum;i++){
            row=sheet.getRow(i);
            if(row==null){
                continue;
            }
            try{
                key= row.getCell(1).getStringCellValue();
                value=row.getCell(2).getCellStyle();
            }catch (Exception e){
                continue;
            }
            if(StringUtils.isBlank(key) || value==null){
                continue;
            }
            //标题格式
            if(StyleConstant.TITLE.equals(key)){
                titleStyle=currWorkbook.createCellStyle();
                titleStyle.cloneStyleFrom(value);
            }else if(StyleConstant.TITLE_.equals(key)){
                titleStyle_=currWorkbook.createCellStyle();
                titleStyle_.cloneStyleFrom(value);
            }else if(StyleConstant.TITLE_1.equals(key)){
                titleStyle_1=currWorkbook.createCellStyle();
                titleStyle_1.cloneStyleFrom(value);
            }else if(StyleConstant.TITLE_2.equals(key)){
                titleStyle_2=currWorkbook.createCellStyle();
                titleStyle_2.cloneStyleFrom(value);
            }else if(StyleConstant.HEADER.equals(key)){
                headerStyle=currWorkbook.createCellStyle();
                headerStyle.cloneStyleFrom(value);
            }else if(StyleConstant.CONTENT.equals(key)){
                contentStyle=currWorkbook.createCellStyle();
                contentStyle.cloneStyleFrom(value);
            }
        }

        try {
            tplInStream.close();
            tplInStream=null;
        } catch (IOException e) {
        }


    }

    public CellStyle getTitleStyle() {
        return titleStyle;
    }

    public CellStyle getTitleStyle_1() {
        return titleStyle_1;
    }

    public CellStyle getTitleStyle_2() {
        return titleStyle_2;
    }

    public CellStyle getHeaderStyle() {
        return headerStyle;
    }

    public CellStyle getTitleStyle_() {
        return titleStyle_;
    }

    public CellStyle getContentStyle() {
        return contentStyle;
    }

    private void _setTemplateInputStream(String selfTemplatePath,boolean isXSSFWorkbook) throws FileNotFoundException {

        if(StringUtils.isBlank(selfTemplatePath)){
            String my_templateName="";
            if(isXSSFWorkbook){//如果是2007版本的
                my_templateName=templateName2007;
                tplInStream = ExcelStyle.class.getResourceAsStream(templateName2007);
            }else{
                my_templateName=templateName;
                tplInStream = ExcelStyle.class.getResourceAsStream(templateName);
            }

            if(tplInStream==null){
                tplInStream=Thread.currentThread().getContextClassLoader().getSystemResourceAsStream(my_templateName);
            }
        }else{
            System.out.println("使用自定义模板"+selfTemplatePath);
            if(tplInStream==null){
                tplInStream=new FileInputStream(selfTemplatePath);
            }
        }

    }

    class StyleConstant{
        public static final  String TITLE    ="Title";
        public static final  String TITLE_  ="Title_";
        public static final  String TITLE_1  ="Title_1";
        public static final  String TITLE_2  ="Title_2";
        public static final  String HEADER   ="Header";
        public static final  String HEADER_1 ="Header_1";
        public static final  String HEADER_2 ="Header_2";
        public static final  String HEADER_3 ="Header_3";
        public static final  String HEADER_4 ="Header_4";
        public static final  String CONTENT  ="content";
        public static final  String CONTENT_1="content_1";
        public static final  String CONTENT_2="content_2";
        public static final  String CONTENT_3="content_3";
        public static final  String CONTENT_4="content_4";
        public static final  String INFO     ="info";
        public static final  String INFO_1   ="info_1";
        public static final  String INFO_2   ="info_2";
        public static final  String INFO_3   ="info_3";
    }

}


















